//加载模块
layui.extend({
    dtree: '/dTree/layui_ext/dtree/dtree'
  }).use(['element','layer', 'table', 'code' ,'util', 'dtree', 'form'], function(){
    var dtree = layui.dtree;
    var layer = layui.layer; 
    var $ = layui.jquery;
	var table = layui.table;
	var element = layui.element;
	var util = layui.util;
	$.ajax({
		url:"/articletype/getTrees",
		type:"post",
		dataType:"json",
		data:{},
		success:function(data){
			getdataTree(data);
		}
	}) 
});
//加载树形
function getdataTree(data){
	var dtree = layui.dtree;
	var $=layui.jquery;
	var layer = layui.layer; 
	//渲染
	var DemoTree = dtree.render({
		elem:"#test1",//容器
		data:data,
		toolbar:true,
		initLevel:3,//默认展开
		toolbarFun: {
			addTreeNode: function(treeNode, $div){
				
			    $.ajax({
			        type: "post",
			        data: treeNode,
			        url: "/articletype/insertTree",
			        success: function(result){
			        	location.reload();
			        	layer.msg("添加成功");
			        },
			        error: function(){
			        	layer.msg("添加失败");
			        }
			     });
			},
			editTreeNode: function(treeNode, $div){
				$.ajax({
			        type: "post",
			        data: treeNode,
			        url: "/articletype/updateTree",
			        success: function(result){
			        	location.reload();
			        	layer.msg("修改成功");
			        },
			        error: function(){
			        	layer.msg("修改失败");
			        }
			    });
			},
			delTreeNode: function(treeNode, $div){
				console.log(treeNode);
				$.ajax({
			        type: "post",
			        data: treeNode,
			        url: "/articletype/deleteTree",
			        success: function(result){
			        	location.reload();
			        	layer.msg("删除成功");
			        },
			        error: function(){
			        	layer.msg("删除失败");
			        }
			   	});
			}
		}	
	})
	//绑定节点点击
    dtree.on("node('test1')" ,function(obj){
      layer.msg(JSON.stringify(obj.param.nodeId));
      var nodeId = obj.param.nodeId;
      
    });
}